const app=getApp()
const db = wx.cloud.database()
const directionCollection = db.collection('directions')
Page({
    // data:{
    //     user:null,
    //     list:[],
    //     directions:[],
    //     nums:[],
    //     tab:0,
    //     result:''
    // },
    async onLoad(){
        let res = await directionCollection.where({}).get()
        let directions = res.data.map(v=>{
            return v.name
        })
        let nums = res.data.map(v=>{
            return v.num
        })
        
        res = await wx.cloud.callFunction({
            name:'pc_statistics'
        })
        console.log('res',res)
        let result = res.result.list.find(v=>{
            return v.name ==app.globalData.user.name
        }).choosen


        let tab = directions.indexOf(result)
        let list = []
        directions.forEach(v=>{
            list.push(res.result.list.filter(vv=>{
                return vv.choosen==v
            }))
        })
        console.log('list',list)

        this.setData({
            user:app.globalData.user,
            list,
            result,
            tab,
            directions,
            nums
        })
    },
    rank(){
        wx.redirectTo({
          url: '/pages/work18/rank',
        })
    },
    changeItem: function (e){
        console.log('changeItem',e)
        this.setData({
            tab: e.target.dataset.item
        })
    },
    changeTab: function(e){
        console.log('changeTab',e)
        this.setData({
            tab: e.detail.current
        })
    }
})